<?php

namespace App\Http\Controllers\System;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\ClassBox\globals;
class StaffController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {


        $q_Com_list=DB::select('select id,Name from mycom where Type=4');

        $Com_list=DB::select('select * from mycom where Type=4  and Del=1');//查询所有的公司
        if($Com_list!=NULL){
            $Dep_list=DB::select('select * from mycom where Type=6 and Del=1 and ComId='.$Com_list[0]->id);//查询所有的部门
        }else{
         $Com_list=null;

        } 
        $Group_list="";
        if($Dep_list!=null){
            $Group_list=DB::select('select * from mycom where Type=8 and Del=1 and DepId='.$Dep_list[0]->id);//查询所有的组

        }else{

          $Dep_list=null;
        }
       
        
        $Type=DB::select('select id,Type,Name from usetype where Del=1 and Type=1');// 查询所有的工种

        $Role=DB::select('select * from usetype where Del=1 and Type=2 ORDER BY Name="员工" desc,id desc');// 查询所有的职务
        $banner="";
        $query_show=$this->show(0,$banner);
        $query_show=$this->show(0,$banner);
        return view('system.staff',[
             'Com_list'=>$Com_list, //直接渲染  公司
             'Dep_list'=>$Dep_list, // 部门
             'Group_list'=>$Group_list, // 组
             'query_show'=>$query_show,
             'q_Com_list'=>$q_Com_list,
             'Type'=>$Type,
             'Role'=>$Role

            ]);
    }




 // /**
 //     * 查询个人信息接口
 //     *  deng
 //     * @return \Illuminate\Http\Response
 //     */
 //    public function query($index,$banner){
        
 //        $result=array();

 //        $number=2;// 显示20条信息

 //        $index=$index ? ($index-1)*$number+1 : 1;
 //        $index--;
 //       $query_cost=DB::select('select u.*,mycom.Name as DepId_name,T.Name as T_name,R.Name as R_name from'."`" .'use'. "`".'u  LEFT JOIN mycom  on u.DepId=mycom.id LEFT JOIN usetype T on T.id=u.Type LEFT JOIN usetype R on R.id=u.RoleId where 1=1'.$banner);
 //        //$query_cost=DB::select('select * from '."`" .'use'. "`".'where 1=1'.$banner);
 //        $cost_list=array_reverse($query_cost);  //打乱原来数组的key  生成一个新的数组
 //        $result['total']=count($cost_list);
 //        $result['list']=array_slice($cost_list,$index,$number);
 //        return $result;

 //      }

    /*
*
* select 请求--添加
* deng
*/
    public function select_List(Request $request){

        switch($request->input('Type'))
        {
        case 4:
        $data['data']=DB::table('mycom')->select('id','Name','Type')->where('ComId',$request->input('pid'))->where('Type',6)->where('Del',1)->get();
        if($data['data']!=NULL){
        $data['Group']=DB::table('mycom')->select('id','Name','Type')->where('DepId',$data['data'][0]->id)->where('Type',8)->where('Del',1)->get();
       
        }
        return json_encode($data);
        break;
        case 6:
        $data['data']=DB::table('mycom')->select('id','Name','Type')->where('DepId',$request->input('pid'))->where('Type',8)->where('Del',1)->get();
        return json_encode($data);
        break;
        default:
        return 0;
        }


    }

    /*
    *
    * select 请求--搜索
    * deng
    */
     public function select_query(Request $request){
             
        switch($request->input('Type'))
        {

        case 4:
        $data['data']=DB::table('mycom')->select('id','Name','Type')->where('ComId',$request->input('pid'))->where('Type',6)->get();
        return json_encode($data);
        break;
        case 6:
        $data['data']=DB::table('mycom')->select('id','Name','Type')->where('DepId',$request->input('pid'))->where('Type',8)->get();
        return json_encode($data);
        break;
        default:
        return 0;
        }
        }


    /**
     * 添加|修改  公司人员
     * deng
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Requests\StaffRequest $request)
    {
      $qx='1,2,';
      $jur=$request->input('Jurisdiction');
      if($jur!=NULL){
        $qx.=$jur;
      }else{
        $qx='1,2';
      }
      
   
      if(intval($request->input('id'))>0){



        DB::table('use')->where('id','=',intval($request->input('id')))->update(
            array(
                'ComId'=>intval($request->input('ComId')),
                'DepId'=>intval($request->input('DepId')),
                'GroupId'=>intval($request->input('GroupId')),
                'RoleId'=>intval($request->input('RoleId')),
                'Type'=>intval($request->input('Type')),
                'Name'=>trim($request->input('Name')),
                'WW'=>$request->input('WW'),
                'QQ'=>$request->input('QQ'),
                'Call'=>$request->input('call'),
                'Email'=>$request->input('Email'),
                'Mobile'=>$request->input('Mobile'),
                'UseId'=>0,
                'Commission'=>$request->input('Commission'),
                'Time'=>date('Y-m-d H:i:s',time()),
                'Del'=>1,
                'Jurisdiction'=>$qx,//权限
                'Supplier'=>0  //0 公司人员
            )
        );

          return redirect('system/staff/index')->with('successful','修改成功！');



      }else{

       $insert=array(
          'ComId'=>intval($request->input('ComId')),
          'DepId'=>intval($request->input('DepId')),
          'GroupId'=>intval($request->input('GroupId')),
          'RoleId'=>intval($request->input('RoleId')),
          'Type'=>intval($request->input('Type')),
          'Name'=>trim($request->input('Name')),
          'WW'=>$request->input('WW'),
          'QQ'=>$request->input('QQ'),
          'Call'=>$request->input('call'),
          'Email'=>$request->input('Email'),
          'Mobile'=>$request->input('Mobile'),
          'UseId'=>0,
          'Commission'=>$request->input('Commission'),
          'Time'=>date('Y-m-d H:i:s',time()),
          'Del'=>1,
          'Jurisdiction'=>$qx,//权限
          'Supplier'=>0  //0 公司人员
          );

       
         DB::table('use')->insert($insert);

          return redirect('system/staff/index')->with('successful','添加成功！');

      }

        

    }


     /**
     * 分页---接口
     *  deng
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show_list(Request $request)
    {
        $result=array();
        $index= $request->input('index');

       //  搜索条件
       $banner=""; //拼接
       $ComId=$request->input('ComId');//公司
       if($ComId!=NULL){
       $banner .=" and u.ComId=".$ComId;
       }
       $DepId=$request->input('DepId');//部门
       if($DepId!=NULL){
       $banner .=" and u.DepId=".$DepId;
       }
       $GroupId=$request->input('GroupId');//组
       if($GroupId!=NULL){
           $banner .=" and u.GroupId=".$GroupId;
       }
       $Name=$request->input('Name');//姓名
       if($Name!=NULL){
         $banner .=" and u.Name regexp '".$Name."'";
       }
       if($Name!=NULL){
         $banner .=" or u.WW regexp '".$Name."'";//旺旺
       }
         
        
       $query_list=$this->show($index,$banner);

        //  $query_list=$this->query($index,$banner);
        
       $result["Is_null"] ="true";
       $result["data"] = $query_list;
       return json_encode($result);
      
      

    }


  /**
  * 员工显示页面
  *  deng
  */

  public function show($index,$banner){
     
        $result=array();
        $number=10;// 显示20条信息
        $index=$index ? ($index-1)*$number+1 : 1;
        $index--;
        $query_cost=DB::select('select u.*,mycom.Name as DepId_name,T.Name as T_name,R.Name as R_name from `use`u 
                                 LEFT JOIN mycom on if(u.GroupId=0 && u.DepId!=0,u.DepId=mycom.id,
                                  if(u.GroupId=0 && u.DepId=0,u.ComId=mycom.id,u.GroupId=mycom.id))
                                   LEFT JOIN usetype T on T.id=u.Type 
                                    LEFT JOIN usetype R on R.id=u.RoleId where 1=1'.$banner);

        $number=10;// 显示20条信息
        $index=$index ? ($index-1)*$number+1 : 1;
        $index--;
        $query_cost=DB::select('select u.*,mycom.Name as DepId_name,T.Name as T_name,R.Name as R_name from `use`u 
                                 LEFT JOIN mycom on if(u.GroupId=0 && u.DepId!=0,u.DepId=mycom.id,
                                  if(u.GroupId=0 && u.DepId=0,u.ComId=mycom.id,u.GroupId=mycom.id))
                                   LEFT JOIN usetype T on T.id=u.Type 
                                    LEFT JOIN usetype R on R.id=u.RoleId where 1=1'.$banner);
        $cost_list=array_reverse($query_cost);  //打乱原来数组的key  生成一个新的数组
        $result['total']=count($cost_list);
        $result['list']=array_slice($cost_list,$index,$number);
        return $result;
  }
    /**
     * 恢复与停用
     * deng
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function restore_disable (Request $request)
    {
       $result=array();
       $Del=intval($request->input('Del'));
       $id=intval($request->input('id'));

       if(empty($Del) && empty($id)){

        $result['Success'] = 'false';
        return json_encode($result);
       }
       if($Del=='1' && $id!=NULL){
           DB::table('use')->where('id',$id)->update(
             array(
               'Del' =>0
                )
               );

       }

       if($Del=='0' && $id!=NULL){
           DB::table('use')->where('id',$id)->update(
             array(
               'Del' =>1
                )
               );

       }

        $result['Success'] = 'true';
        return json_encode($result);
    }

 
    /**
     * 重置密码
     * deng
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function reset_password(Request $request)
    {
        $id=$request->input('id');
        if(empty($id)){
          $result['Success'] = 'false';
          return json_encode($result);

        }

        DB::table('use')->where('id',$id)->update(
            array(
               'Pwd' =>md5(123456)
                )
            );

          $result['Success'] = 'true';
          return json_encode($result);

    }

    /**
     * 编辑按钮
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request)
    {
        $result=array();
        $id=$request->input('id');
        if(empty($id)){
        $result['masege'] = 'false';
        return json_encode($result);
        }
        $result['data']=DB::table('use')->where('id',$id)->get();
        $result['ComId']=DB::table('mycom')->select('id','Name','Type')->where('id',$result['data'][0]->ComId)->get();//公司
        $result['DepId']=DB::table('mycom')->select('id','Name','Type')->where('ComId',$result['data'][0]->ComId)->where('Type',6)->get();//部门
        $result['GroupId']=DB::table('mycom')->select('id','Name','Type')->where('DepId',$result['data'][0]->DepId)->where('Type',8)->get();//组 
        $result['RoleId']=DB::table('usetype')->select('id','Name','Type')->where('id',$result['data'][0]->RoleId)->get();//职务
        $result['Type']=DB::table('usetype')->select('id','Name','Type')->where('id',$result['data'][0]->Type)->get();//工种 

       return json_encode($result);



    }
     /*
     *  查看员工详情
     *  deng
     *
     */
     public function See_details(Request $request){


        $id=$request->input('id');
        $result=array();
        $result['data']=DB::table('use')->where('id',$id)->get();
        $result['ComId']=DB::table('mycom')->select('id','Name','Type')->where('id',$result['data'][0]->ComId)->get();//公司
        $result['DepId']=DB::table('mycom')->select('id','Name','Type')->where('id',$result['data'][0]->DepId)->where('Type',6)->get();//部门
        $result['GroupId']=DB::table('mycom')->select('id','Name','Type')->where('id',$result['data'][0]->GroupId)->where('Type',8)->get();//组 
        $result['RoleId']=DB::table('usetype')->select('id','Name','Type')->where('id',$result['data'][0]->RoleId)->get();//职务
        $result['Type']=DB::table('usetype')->select('id','Name','Type')->where('id',$result['data'][0]->Type)->get();//工种 
        return json_encode($result);

     }
  


}
